from threading import Thread,Lock  # 1.导入Lock
# 互斥锁：保证同一时间只能执行一个线程
# 2.创建锁对象
lock = Lock()
num = 0
count = 1000000
def add1():
    # 加锁
    lock.acquire()
    global num
    print(f"add1的初始值为{num}")
    for i in range(count):
        num+=1
    print(f"add1的最终值为{num}")
    # 解锁
    lock.release()

def add2():
    lock.acquire()
    global num
    print(f"add2的初始值为{num}")
    for i in range(count):
        num+=1
    print(f"add2的最终值为{num}")
    lock.release()

if __name__ == '__main__':
    t1 = Thread(target=add1)
    t2 = Thread(target=add2)
    t1.start()
    t2.start()